package com.haixiaoke.saas.contract.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.constant.ContractConstants;
import com.haixiaoke.saas.common.constant.ProprietorConstants;
import com.haixiaoke.saas.common.constant.TenantConstants;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.contract.domain.Proprietor;
import com.haixiaoke.saas.contract.domain.ProxyService;
import com.haixiaoke.saas.contract.fadada.service.FaDaDaService;
import com.haixiaoke.saas.contract.mapper.ProxyServiceMapper;
import com.haixiaoke.saas.contract.service.IProprietorService;
import com.haixiaoke.saas.contract.service.IProxyServiceService;
import com.haixiaoke.saas.department.domain.ElectronicVisa;
import com.haixiaoke.saas.department.service.IElectronicVisaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 代理服务信息Service业务层处理
 *
 * @author xiaolong
 * @date 2024-06-06
 */
@Service
public class ProxyServiceServiceImpl implements IProxyServiceService {
    @Autowired
    private ProxyServiceMapper proxyServiceMapper;
    @Autowired
    private IProprietorService proprietorService;
    @Autowired
    private FaDaDaService faDaDaService;
    @Autowired
    private IElectronicVisaService electronicVisaService;

    /**
     * 查询代理服务信息
     *
     * @param proxyServiceId 代理服务信息主键
     * @return 代理服务信息
     */
    @Override
    public ProxyService selectProxyServiceByProxyServiceId(Long proxyServiceId) {
        return proxyServiceMapper.selectProxyServiceByProxyServiceId(proxyServiceId);
    }


    /**
     * 查询代理服务信息列表
     *
     * @param proxyService 代理服务信息
     * @return 代理服务信息
     */
    @Override
    public List<ProxyService> selectProxyServiceList(ProxyService proxyService) {
        return proxyServiceMapper.selectProxyServiceList(proxyService);
    }

    /**
     * 新增代理服务信息
     *
     * @param proxyService 代理服务信息
     * @return 结果
     */
    @Override
    public int insertProxyService(ProxyService proxyService) {
        Proprietor proprietor = proprietorService.selectProprietorByProprietorId(proxyService.getProprietorId());
        if (ObjectUtil.isEmpty(proprietor)) {
            throw new ServiceException("未找到业主信息");
        }
        proprietor.setProxyServiceStatus(ProprietorConstants.PROXY_SERVICE_STATUS_UNFINISHED);
        int i = proprietorService.updateProprietor(proprietor);
        if (i != 1) {
            throw new ServiceException("更新业主状态失败");
        }
        proxyService.setConUserId(proprietor.getConUserId());
        proxyService.setCreateTime(DateUtils.getNowDate());
        return proxyServiceMapper.insertProxyService(proxyService);
    }

    /**
     * 修改代理服务信息
     *
     * @param proxyService 代理服务信息
     * @return 结果
     */
    @Override
    public int updateProxyService(ProxyService proxyService) {
        proxyService.setUpdateTime(DateUtils.getNowDate());
        return proxyServiceMapper.updateProxyService(proxyService);
    }

    /**
     * 批量删除代理服务信息
     *
     * @param proxyServiceIds 需要删除的代理服务信息主键
     * @return 结果
     */
    @Override
    public int deleteProxyServiceByProxyServiceIds(Long[] proxyServiceIds) {
        return proxyServiceMapper.deleteProxyServiceByProxyServiceIds(proxyServiceIds);
    }

    /**
     * 删除代理服务信息信息
     *
     * @param proxyServiceId 代理服务信息主键
     * @return 结果
     */
    @Override
    public int deleteProxyServiceByProxyServiceId(Long proxyServiceId) {
        return proxyServiceMapper.deleteProxyServiceByProxyServiceId(proxyServiceId);
    }

    @Override
    public ProxyService selectProxyServiceByProprietorId(Long proprietorId) {
        return proxyServiceMapper.selectProxyServiceByProprietorId(proprietorId);
    }

    @Override
    public String scanAuth(Long proxyServiceId) {
        ProxyService proxyService = proxyServiceMapper.selectProxyServiceByProxyServiceId(proxyServiceId);
        if (ObjectUtil.isEmpty(proxyService)) {
            throw new ServiceException("代理服务不存在");
        }
        if (ProprietorConstants.ELECTRONIC_VISA_STATUS_AUTH.equals(proxyService.getElectronicVisaStatus())) {
            throw new ServiceException("电子签证已授权");
        }
        Proprietor proprietor = proprietorService.selectProprietorByProprietorId(proxyService.getProprietorId());
        if (ObjectUtil.isEmpty(proprietor)) {
            throw new ServiceException("未找到业主信息");
        }
        String authUrl = "";
        ElectronicVisa electronicVisa = electronicVisaService.selectElectronicVisaByProprietorUserId(proprietor.getConUserId());
        if (ObjectUtil.isEmpty(electronicVisa)) {
            electronicVisa = new ElectronicVisa();
            electronicVisa.setProprietorUserId(proprietor.getConUserId());
            if (ContractConstants.PROPRIETOR_TYPE_PERSON.equals(proprietor.getProprietorType())) {
                //个人业主
                electronicVisa.setElectronicVisaType(TenantConstants.ELECTRONIC_VISA_TYPE_PERSON);
            } else if (ContractConstants.PROPRIETOR_TYPE_ENTERPRISE.equals(proprietor.getProprietorType())) {
                //企业业主
                electronicVisa.setElectronicVisaType(TenantConstants.ELECTRONIC_VISA_TYPE_ENTERPRISE);
            }
            electronicVisaService.insertElectronicVisa(electronicVisa);

            if (ObjectUtil.isEmpty(electronicVisa.getVisaId())) {
                throw new ServiceException("创建电子签证失败");
            }
        }

        //调用法大大接口获取授权地址
        if (ContractConstants.PROPRIETOR_TYPE_PERSON.equals(proprietor.getProprietorType())) {
            //个人业主
            authUrl = faDaDaService.getUserAuthUrl(String.valueOf(electronicVisa.getVisaId()), proprietor.getProprietorMobile());
        } else if (ContractConstants.PROPRIETOR_TYPE_ENTERPRISE.equals(proprietor.getProprietorType())) {
            //企业业主
            authUrl = faDaDaService.getCorpAuthUrl(String.valueOf(electronicVisa.getVisaId()), proprietor.getProprietorMobile());
        }
        return authUrl;
    }

    @Override
    public int updateElectronicVisaStatusAuth(Long conUserId) {
        return proxyServiceMapper.updateElectronicVisaStatusAuth(conUserId);
    }

    @Override
    public ProxyService selectProxyServiceByConUserId(Long conUserId) {
        return proxyServiceMapper.selectProxyServiceByConUserId(conUserId);
    }
}
